/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vn.com.sml.SEFM.BUS.MerchantBUS;
import vn.com.sml.SEFM.BUS.UserBUS;
import vn.com.sml.SEFM.BUS.UserRolesBUS;
import vn.com.sml.SEFM.BUS.UserStatusBUS;
import vn.com.sml.SEFM.domain.Merchant;
import vn.com.sml.SEFM.domain.User;
import vn.com.sml.SEFM.domain.UserRoles;
import vn.com.sml.SEFM.domain.Userstatus;

/** 
 *
 * @author Tunp
 */
public class userlistController extends SimpleFormController implements Controller{
    private UserBUS userBUS = new UserBUS();
    private MerchantBUS dvcntBUS = new MerchantBUS();
    private UserRolesBUS userrolesBUS = new UserRolesBUS();
    private UserStatusBUS userstatusBUS = new UserStatusBUS();

    public void setUserstatusBUS(UserStatusBUS userstatusBUS) {
        this.userstatusBUS = userstatusBUS;
    }

    public void setUserBUS(UserBUS userBUS) {
        this.userBUS = userBUS;
    }
    public void setDvcntBUS(MerchantBUS dvcntBUS) {
        this.dvcntBUS = dvcntBUS;
    }

    public void setUserrolesBUS(UserRolesBUS userrolesBUS) {
        this.userrolesBUS = userrolesBUS;
    }
    public userlistController() {
        //Initialize controller properties here or 
        //in the Web Application Context

//        setCommandClass(User.class);
//        setCommandName("user");
//        setSuccessView("DetailUser");
//        setFormView("ListUsers");
    }
    
       
    @Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
 
                String id = (arg0.getParameter("userId"));
                String del_id = (arg0.getParameter("del_id"));
                
                // Box Search
                String search_col = (arg0.getParameter("search_col"));
                String search_content = (arg0.getParameter("search_text"));
                               
                if(id==null){
                    if(del_id != null)
                    {
                        int dId = Integer.parseInt(del_id);
                        userBUS.DeleteUser(dId);
                    }
                    List<User> lstUs = null;
                    if(search_content != null)
                    {
                        lstUs = userBUS.listUser(search_col,search_content);
                    }
                    else
                    {
                        lstUs = userBUS.listUser();
                    }                    
                    ModelAndView modelAndView = new ModelAndView("ListUsers");
                    modelAndView.addObject("lstUs", lstUs);
                    
                    return modelAndView;
                }
                else {
                    int uId = Integer.parseInt(id);
                    
                    ModelAndView mv = new ModelAndView("DetailUser");
                    User us = userBUS.getUserById(uId);
                    mv.addObject("user",us);
                    
                    return mv;
                }
	}

//     @Override
//    protected void doSubmitAction(Object command) throws Exception {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }

    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    /*
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request, 
    HttpServletResponse response, 
    Object command, 
    BindException errors) throws Exception {
    ModelAndView mv = new ModelAndView("DetailUser");
    //Do something...

    
    return mv;
    }
     */
}
